package chixing.day16;

import java.io.*;
import java.lang.invoke.VarHandle;

public class InOutWriterRead {
    public static void main(String[] args) {
//        outPutDemo();
//        inPutDemo();
//        writeDemo();
//        readDemo();
    }
    /**
     * 字节写入流
     */
    public static void outPutDemo(){
        //1.创建写入
        OutputStream os = null;
        try {
            os = new FileOutputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\byte_hello.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //2.写操作
        String str = "xin xie de nei rong";
        byte[] bytes = str.getBytes();
        try {
            os.write(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally { //3.关闭流
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    /**
     * 字节读取流
     */
    public static void inPutDemo(){
        //1.创建读取流
        FileInputStream is = null;
        try {
            is = new FileInputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\byte_hello.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //2.读操作
        byte[] bytes = null;
        try {
            bytes = new byte[is.available()];
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int temp;
        int i = 0;
        try {
            while ((temp = is.read()) != -1){
                bytes[i++] = (byte) temp;
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {//关闭流
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        String str = new String(bytes);
        System.out.println(str);
    }

    /**
     * 字符写入流
     */
    public static void writeDemo(){
        //1.创建一个写入流
        FileWriter writer = null;
        try {
            writer = new FileWriter("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\char_hello.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //2.写操作
        try {
            writer.write("zi fu xie ru liu");
        } catch (IOException e) {
            e.printStackTrace();
        }finally { //3.关闭流
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    /**
     * 字符读取流
     */
    public static void readDemo(){
        //1.创建一个读取流
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\char_hello.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        //2.读操作
        char[] chars = new char[1024];
        int temp;
        int i = 0;
        try {
            while ((temp=fileReader.read()) != -1){
                chars[i++] = (char) temp;
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {//3.关闭流
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        String str = new String(chars,0,i);
        System.out.println(str);
    }
}
